package com.sailfish.springbootdemo.configs;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.ServletContext;
import java.io.File;
import java.net.URL;

@Configuration
public class MyInterceptorConfig implements WebMvcConfigurer {

    /*@Value("${personal.config.file.host-prefix-url}")
    private String hostPrefixUrl ;*/

    @Value("${personal.config.file.file-save-url}")
    private String fileSaveUrl;

    @Bean
    public HandlerInterceptor getInterceptor() {
        return new MyInterceptor();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //和页面有关的静态目录都放在项目的static目录下
        // registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        //上传的图片在D盘下的OTA目录下，访问路径如：http://localhost:8081/OTA/d3cf0281-bb7f-40e0-ab77-406db95ccf2c.jpg
        //其中OTA表示访问的前缀。"file:D:/OTA/"是文件真实的存储路径
        registry.addResourceHandler("/capacityFiles/**").addResourceLocations("file:" + fileSaveUrl);
        registry.addResourceHandler("/digitalFiles/**").addResourceLocations("file:" + fileSaveUrl);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册拦截器，要声明拦截器对象和要拦截的请求
        registry.addInterceptor(getInterceptor())
                .addPathPatterns("/**") //所有路径都被拦截
                .excludePathPatterns("/knowledgeBaseApi/**") // 知识库推送第三方接口暴露
                .excludePathPatterns("/sso/**") // sso登录接口暴露
                .excludePathPatterns("/thirdApiSend/**") // 第三方接口暴露
                .excludePathPatterns("/thirdApiSend2/**") // 第三方接口暴露
                .excludePathPatterns("/thirdApiH3c/v1/**") // 第三方接口暴露
                .excludePathPatterns("/pcbNewTechnology/test") // 第三方接口暴露
                .excludePathPatterns("/timer/**") // 排除
                .excludePathPatterns("/pcbCost/getByFactory") // 排除用户登录请求
                .excludePathPatterns("/pcbCost/getAllCostList") // 排除用户登录请求
                .excludePathPatterns("/pcbCapacityMatch/getData") // 排除用户登录请求
                .excludePathPatterns("/pcbCapacityParam/getAll") // 排除用户登录请求
                .excludePathPatterns("/pcbCapacityMatch/outputParamTemplate") // 排除用户登录请求
                .excludePathPatterns("/pcbParentFactoryInfo/getBySonList") // 排除用户登录请求
                .excludePathPatterns("/pcbCapacityParam/getParamList") // 排除用户登录请求
                .excludePathPatterns("/pcbCapacityParam/import") // 排除用户登录请求
                .excludePathPatterns("/pcbCapacityParam/outputParamTemplate") // 排除用户登录请求
                .excludePathPatterns("/pcbCapacityData/outputCapacityComparison") // 排除用户登录请求
                .excludePathPatterns("/pcbCapacityParam/importParamTemplate") // 排除用户登录请求

                .excludePathPatterns("/user/login") // 排除用户登录请求
                .excludePathPatterns("/test/**") // 排除
                .excludePathPatterns("/common/uploadPicture") // 排除上传图片
                .excludePathPatterns("/common/uploadFile") // 排除上传文件
                .excludePathPatterns("/digitalFiles/uploadPicture/**")
                .excludePathPatterns("/digitalFiles/uploadFile/**")
                .excludePathPatterns("/digitalFiles/templates/**")
                .excludePathPatterns("/digitalFiles/pcb/**")
                .excludePathPatterns("/digitalFiles/exportExcels/**")
                .excludePathPatterns("/digitalFiles/commonUploadFiles/**")
                .excludePathPatterns("/capacityFiles/uploadPicture/**")
                .excludePathPatterns("/capacityFiles/uploadPicture/pcb/**")
                .excludePathPatterns("/capacityFiles/uploadFile/**")
                .excludePathPatterns("/capacityFiles/templates/**")
                .excludePathPatterns("/capacityFiles/pcb/**")
                .excludePathPatterns("/capacityFiles/exportExcels/**")
                .excludePathPatterns("/capacityFiles/commonUploadFiles/**")
                .excludePathPatterns("/capacityFiles/commonUploadFiles/pcb/**")
                .excludePathPatterns("/capacityFiles/uploadPicture/**")
                .excludePathPatterns("/capacityFiles/uploadFile/**")
                .excludePathPatterns("/capacityFiles/templates/**")
                .excludePathPatterns("/capacityFiles/pcb/**")
                .excludePathPatterns("/capacityFiles/knowledgeBase/**")
                .excludePathPatterns("/capacityFiles/exportExcels/**")
                .excludePathPatterns("/capacityFiles/commonUploadFiles/**")
        ;
    }
}
